package com.dyh.controller;

import com.dyh.po.illness;
import com.dyh.service.illService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
public class illController {
    @Autowired
    illService illservice;


    @RequestMapping(value = "illList")
    public String insList(Model model){
        List<illness> ill = illservice.findAllIll();
        System.out.println(ill);
        model.addAttribute("illList", ill);
        return "/illness/illlist";
    }


    @RequestMapping(value = "illEdit")
    public ModelAndView toIllEdit(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        ModelAndView mv = new ModelAndView();
        String illid = request.getParameter("i_id");
        illness ill = illservice.findIllById(illid);
        mv.addObject("Illness",ill);
        mv.setViewName("illness/illEdit");
        return mv;
    }

    @RequestMapping(value = "illEditAction")
    public String editUser(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        String illid = request.getParameter("illid");
        String illname = request.getParameter("illname");
        String pym = request.getParameter("pym");
        int row = illservice.editIll(illid,illname,pym);
        if(row>0){
            return "redirect:/illList";
        }else{
            return "institute/illEdit";
        }
    }


    @RequestMapping(value = "illAdd")
    public String toIllAdd(){
        return "illness/illAdd";
    }


    @RequestMapping(value = "illAddAction")
    public String addIll(HttpServletRequest request,Model model){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        String illid = request.getParameter("illid");
        String illname = request.getParameter("illname");
        String pym = request.getParameter("pym");
        int row=illservice.addIll(illid,illname,pym);
        System.out.println("row="+row);
        return "redirect:/illList";
    }

    @RequestMapping(value = "illDeleteAction")
    public String deleteInst(HttpServletRequest request){
        String illid = request.getParameter("i_id");
        int row = illservice.deleteIll(illid);
        return "redirect:/illList";
    }
}
